//
//  SeekResultRow3Cell.m
//  ebook
//
//  Created by mt on 17/1/10.
//  Copyright © 2017年 Moton. All rights reserved.
//

#define KHeaderId @"headerId"
#define kCellId   @"cellId"

#import "NativeStoreRecommendCell.h"
#import "UIImageView+WebCache.h"
#import "Book.h"

@interface NativeStoreRecommendCell ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation NativeStoreRecommendCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self drawView];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)drawView {
    
    [self addSubview:self.collectionView];
}

- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.headerReferenceSize = CGSizeMake(MTScreenW, 55);
        
        layout.minimumLineSpacing = 0;//上下
        layout.minimumInteritemSpacing = 10;//左右
        layout.itemSize = CGSizeMake((MTScreenW-50)/4, kNativeStoreRecommendCellWidth*1.3+40);
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, MTScreenW, kNativeStoreRecommendCellHeight) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.alwaysBounceVertical = NO;
        _collectionView.bounces = NO;
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KHeaderId];
        [_collectionView registerClass:[UICollectionViewCell class]  forCellWithReuseIdentifier:kCellId];
    }
    return _collectionView;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellId forIndexPath:indexPath];
    Book *book = _dataArray.count>0?_dataArray[indexPath.row]:nil;
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = cell.bounds;
    imageView.height = cell.height-40;
    [cell addSubview:imageView];
    [imageView sd_setImageWithURL:[NSURL URLWithString:book.coverpicurl]];

    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:14.0];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.numberOfLines=2;
    titleLabel.textColor = [UIColor darkGrayColor];
    titleLabel.text = book.bookname;
    titleLabel.frame = CGRectMake(0, cell.height-40, cell.width, 40);
    [cell addSubview:titleLabel];
    
    return cell;
}

- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionReusableView *reusableview = nil;
    
    if (kind == UICollectionElementKindSectionHeader) {
        
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KHeaderId forIndexPath:indexPath];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, headerView.width, headerView.height)];
        titleLabel.text = _hotSearchTitle;
        titleLabel.font = [UIFont systemFontOfSize:16.0];
        titleLabel.textColor = [UIColor darkGrayColor];
        [headerView addSubview:titleLabel];
        
        UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        moreBtn.frame = CGRectMake(MTScreenW-110, 0, 100, headerView.height);
        [moreBtn setTitle:@"更多" forState:UIControlStateNormal];
        [moreBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        moreBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        moreBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [moreBtn setImage:[UIImage imageNamed:@"book_arrow"] forState:UIControlStateNormal];
        moreBtn.imageEdgeInsets = UIEdgeInsetsMake(0, moreBtn.width-moreBtn.imageView.width, 0, 0);
        moreBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, moreBtn.imageView.width);
        [moreBtn addTarget:self action:@selector(moreBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [headerView addSubview:moreBtn];
        
        reusableview = headerView;
    }
    
    return reusableview;
}

- (void)moreBtnClick:(UIButton *)btn {
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NativeOrderlistModel *book = _dataArray[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(nativeStoreRecommendCell:selectedWithBook:)]) {
        [self.delegate nativeStoreRecommendCell:self selectedWithBook:book];
    }
}

- (void)setResultCell2Array:(NSArray *)resultCell2Array {
    _dataArray = resultCell2Array;
    [_collectionView reloadData];
}


@end
